package com.kensure.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.frame.ResultInfo;
import co.kensure.frame.ResultRowInfo;
import co.kensure.http.RequestUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kensure.shike.weixinpay.model.WXPrePay;
import com.kensure.shike.weixinpay.service.WXPayinfoService;
import com.kensure.shike.weixinpay.utils.XmlUtil;

@Controller
@RequestMapping("/pay")
public class PayController {

	@Resource
	private WXPayinfoService wXPayinfoService;

	@RequestMapping(value = "/wx/wxapp.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultInfo wxapp(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Double money = json.getDouble("money");
		JSONObject nextAction = JSONObject.parseObject(json.getString("nextAction"));
		String type = nextAction.getString("type");
		String spbill_create_ip = RequestUtils.getClientIp(req);
		WXPrePay prapay = null;
		if ("order".equalsIgnoreCase(type)) {
			Long id = nextAction.getLong("id");
			prapay = wXPayinfoService.addOrder(id, money, spbill_create_ip);
		} else if ("vip".equalsIgnoreCase(type)) {
			Long id = nextAction.getLong("id");
			prapay = wXPayinfoService.addVip(id, spbill_create_ip);
		} else if ("CZ".equalsIgnoreCase(type)) {
			Long id = nextAction.getLong("id");
			prapay = wXPayinfoService.addChongzhi(id, spbill_create_ip);
		} else {
			BusinessExceptionUtil.threwException("没有该类型支付");
		}

		return new ResultRowInfo(prapay);
	}

	/**
	 * 预支付时填写的 notify_url ，支付成功后的回调接口
	 * 
	 * @param request
	 */
	@RequestMapping(value = "/wx/paycallback.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String paycallback(HttpServletRequest request, HttpServletResponse rep) {
		rep.setContentType("text/xml");
		String strXML = XmlUtil.getPostStr(request);
		System.out.println("strXML==" + strXML);
		if (StringUtils.isNotBlank(strXML)) {
			Map<String, String> dataMap = XmlUtil.xmlToMap(strXML);
			String result_code = dataMap.get("result_code");
			String transaction_id = dataMap.get("transaction_id");
			String out_trade_no = dataMap.get("out_trade_no");
			if ("SUCCESS".equalsIgnoreCase(result_code)) {
				wXPayinfoService.callback(result_code, transaction_id, out_trade_no);
			}
			System.out.println("返回数据" + JSON.toJSONString(dataMap));
		}
		return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
	}

}
